package chances.epg.api.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import chances.epg.api.controller.model.PositionInfo;
import chances.epg.api.controller.model.ResultResponse;
import chances.epg.commons.log.EpgLogFactory;
import chances.epg.data.service.BizPositionService;
import chances.epg.entity.biz.BizPosition;

@RestController
public class PositionDataController extends AbstractDataController{
	private static Logger logger = EpgLogFactory.getSystemLogger();
	
	@Autowired
	private BizPositionService bizPositionService;
	
	@RequestMapping(value = "/position/{positionCode}") 
    public ResultResponse<PositionInfo> getPosition (@PathVariable String positionCode,ModelMap model, HttpServletRequest request,HttpServletResponse response) throws IOException {
		BizPosition position = null;
		int result = SUCCESS;
		
		try{
			position = bizPositionService.getBizPositionByCode(positionCode);
		    if(position == null) {
		    	result = OBJ_NOT_FOUND;
		    }
		    
		}catch(DataAccessException ex){
			result = DB_ERROR;
			logger.error(ex.getMessage(),ex);
		}catch(Throwable ex){
			result = ERROR;
			logger.error(ex.getMessage(),ex);
		}
		return new ResultResponse<PositionInfo>(result, new PositionInfo(position));
		
	}
}
